#!/usr/bin/env bash
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
unset color_prompt force_color_prompt

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

alias ..='cd ..'
alias ...='cd ../..'
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ws='cd /workdir'

####################################################################################################
export CPU_NUM=`cat /proc/cpuinfo | grep processor | wc -l`
export WORK=/workdir
export BUILD_OPT="source meta-arene-ivi/scripts/init-build-env --share-dl /workdir/download --buildhistory"
export ARENE_IVI_REPO="https://github.tmc-stargate.com/arene-os-services-cockpit-tmc-wa/arene-ivi-repo.git"
export PROP_QCOM="/prop/apps_apps_proc_with_oss_repos.tar.bz2"
export PROP_RCAR1="/prop/R-Car_Gen3_Series_Evaluation_Software_Package_for_Linux-20191206.tar.gz"
export PROP_RCAR2="/prop/R-Car_Gen3_Series_Evaluation_Software_Package_of_Linux_Drivers-20191021.tar.gz"

init-repo() {
    [ -d /workdir/meta-arene-ivi ] || {
       repo init -u ${ARENE_IVI_REPO} --no-clone-bundle
       repo sync -c --no-clone-bundle -j ${CPU_NUM}
    }

    [ -d /workdir/download ] || mkdir /workdir/download
    [ -d /workdir/.vscode ] || mkdir /workdir/.vscode
    [ -e /workdir/.vscode/settings.json ] || tee -a /workdir/.vscode/settings.json <<EOF >/dev/null
{
    "files.exclude": {
        ".repo": true,
        ".vscode-server": true,
        "download": true,
        "downloadgn": true,
        "vendor": true,
        "bsp": true,
        "build-sim": true,
        "build-qcom": true,
        "build-rcar": true
    }
}
EOF
}

init-sim() {
    ${BUILD_OPT} -m sim -b build-sim -V ${1:-debug}
}

init-qcom() {
    [ -d /workdir/vendor/qcom ] || {
        mkdir -p /workdir/vendor/qcom
        tar xf ${PROP_QCOM} -C /workdir/vendor/qcom/
    }

    ${BUILD_OPT} -m qcom -b build-qcom -V ${1:-debug}
}

init-rcar() {
    [ -d /workdir/vendor/renesas ] || {
        mkdir -p /workdir/vendor/renesas
        tar xf ${PROP_RCAR1} -C /workdir/vendor/renesas
        tar xf ${PROP_RCAR2} -C /workdir/vendor/renesas
    }

    ${BUILD_OPT} -m rcar-gen3-21mm -b build-rcar -V ${1:-debug}
}

####################################################################################################
clear
echo 'USAGE:'
echo '  init-repo:               init the arene-ivi-repo.'
echo '  init-sim debug/release:  init build env for sim.'
echo '  init-qcom debug/release: init build env for qcom.'
echo '  init-rcar debug/release: init build env for rcar.'
